package com.zzf.educenter.controller;


import com.zzf.commonutils.JwtUtils;
import com.zzf.commonutils.R;
import com.zzf.commonutils.orderVo.UcenterMemberOrder;
import com.zzf.educenter.entity.UcenterMember;
import com.zzf.educenter.entity.vo.RegisterVo;
import com.zzf.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-15
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {

        //调用service方法实现登录
        String token = memberService.login(member);

        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getUserInfo")
    public R getMemberInfo(HttpServletRequest request) {

        //调用jwt工具类方法,根据request对象获取头信息,返回用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);

        //查询数据库,根据ID查询用户信息
        UcenterMember member = memberService.getById(id);

        return R.ok().data("userinfo", member);
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {

        //查询数据库,根据ID查询用户信息
        UcenterMember member = memberService.getById(id);

        //把member对象中的值赋值给UcenterMemberOrder类对象
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, memberOrder);

        return memberOrder;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {

        Integer count = memberService.countRegisterDay(day);

        return R.ok().data("countRegister", count);
    }
}

